var Main = Class.create();
Main.prototype = {
	canvas : null,
	ctx : null,
	width : 224,
	height : 384,
	field : null,
	gameEvent : null,
	frameCount : 0,
	doUpdate : false,
	KEY : { RIGHT:39, UP:38, LEFT:37, DOWN:40, X:88, Z:90 },
	input : {
		right: false,
		up: false,
		left: false,
		down: false,
		x: false,
		z: false
	},
	oldInput : null,
	rightRepeatCount : 0,
	leftRepertCount : 0,
	
	initialize : function() {
		this.field = new Field(RES_URL + "block.png");
		this.gameEvent = new GameEvent();
		this.oldInput = Object.clone(this.input);
		sound.playBGM(0, true);
	},
	
	keydown : function(e) {
		switch(e.keyCode) {
			case this.KEY.RIGHT:
				this.input.right = true;
				break;
			case this.KEY.UP:
				this.input.up = true;
				break;
			case this.KEY.LEFT:
				this.input.left = true;
				break;
			case this.KEY.DOWN:
				this.input.down = true;
				break;
			case this.KEY.X:
				this.input.x = true;
				break;
			case this.KEY.Z:
				this.input.z = true;
				break;
		}
		
		Event.stop(e);
	},
	
	keyup : function(e) {
		switch(e.keyCode) {
			case this.KEY.RIGHT:
				this.input.right = false;
				break;
			case this.KEY.UP:
				this.input.up = false;
				break;
			case this.KEY.LEFT:
				this.input.left = false;
				break;
			case this.KEY.DOWN:
				this.input.down = false;
				break;
			case this.KEY.X:
				this.input.x = false;
				break;
			case this.KEY.Z:
				this.input.z = false;
				break;
		}
		
		Event.stop(e);
	},
	
	update : function() {
		for (var i = 0; i < gameEvents.length; i++) {
			switch (gameEvents[i].id) {
				case "GAMEOVER":
					sound.pauseBGM();
					$('buttonStart').style.display = "";
					clearInterval(timerId);
					delete gameEvents[i];
					break;
			}
		}
		gameEvents = gameEvents.compact();
		
		if (this.input.right) {
			if (this.rightRepeatCount == 0) {
				this.field.moveRight();
			}
			this.rightRepeatCount++;
			if (this.rightRepeatCount >= 2) {
				this.rightRepeatCount = 0;
			}
		} else {
			this.rightRepeatCount = 0;
		}
		
		if (this.input.left) {
			if (this.leftRepeatCount == 0) {
				this.field.moveLeft();
			}
			this.leftRepeatCount++;
			if (this.leftRepeatCount >= 2) {
				this.leftRepeatCount = 0;
			}
		} else {
			this.leftRepeatCount = 0;
		}
	
		if (this.input.up && !this.oldInput.up) {
			if (this.field.canRotateBlockLeft()) {
				this.field.rotateBlockLeft();
				sound.playSE(0);
			}
		}
	
		if (this.input.down) {
			this.field.moveDown();
		}
		
		if (this.input.z && !this.oldInput.z) {
			if (this.field.canRotateBlockLeft()) {
				this.field.rotateBlockLeft();
				sound.playSE(0);
			}
		}
		
		if (this.input.x && !this.oldInput.x) {
			if (this.field.canRotateBlockRight()) {
				this.field.rorateBlockRight();
				sound.playSE(0);
			}
		}
		
		this.frameCount++;
		if (this.frameCount % 20 == 0) {
			this.field.moveDown();
		}
		
		this.ctx.clearRect(0, 0, this.width, this.height);
		this.field.drawField(this.ctx);
		
		this.oldInput = Object.clone(this.input);
	},
	
	timerExpired : function() {
		if (!this.doUpdate) {
			this.doUpdate = true;
			this.update();
		}
		this.doUpdate = false;
	}
};

var gameEvents;
var sound;
var main;

function gameStart() {
	$('buttonStart').style.display = "none";
	$('canvas1').style.display = "";
	gameEvents = new Array()
	sound = new Sound();
	main = new Main();
	main.canvas = $("canvas1");
	main.ctx = main.canvas.getContext("2d");
	Event.observe(window, 'keydown', function(e) { main.keydown(e); }, false);
	Event.observe(window, 'keyup', function(e) { main.keyup(e); }, false);
	timerId = setInterval(function() { main.timerExpired(); }, 32);
}

function init() {
	$('buttonStart').style.display = "";
}

if ("gadgets" in window) {
	gadgets.util.registerOnLoadHandler(init);
	RES_URL = "http://park19.wakwak.com/~usapyon/tetris/res/";
} else {
	window.onload = init;
	RES_URL = "res/";
}
